/**
 * 
 */
function fillLangNames(form){
	var oriIndex = form.langOri.selectedIndex;
	form.langOriName.value = form.langOri.options[oriIndex].text;
	
	var destIndex = form.langDest.selectedIndex;
	form.langDestName.value = form.langDest.options[destIndex].text;
}

function validateForm(form){
	if(form.langOri.value!=form.langDest.value){
		setLoadingGif();
		return true;
	}
	else{
		alert(ERROR_MSG);
		return false;
	}
}

function setLoadingGif(){
	var count = 20;
	$("#submit").hide();
	$("#loadImage").show();
	$("#loadText").html(count);
	$("#loadText").show();
	
	var countdown = setInterval(function(){
		$("#loadText").html(count);
		if(count == 0){
			$("#loadImage").hide(300);
			$("#loadText").hide(300);
			$("#submit").show(300);
			clearInterval(countdown);
		}
		count --;
	}, 1000);
}

$(function() {
	//document.getElementById("updateButton").disabled = true;
	$('#add_button').click(function (){
		window.open('index.php?option=com_hizkimizki&task=createTranslation', '_self');
	});

	$('#updateButton').click(function (){
		if(validateForm()){
			document.getElementById("updateButton").disabled = true;
			document.getElementById("activeButton").disabled = true;
			document.getElementById("backButton").disabled = true;
			document.getElementById("transForm").submit();
		}
	});

	$("#transForm").submit(validateForm());
});

function validateForm(){
	var result = true;
	if($('#name').val() == ''){
		result = false;
		$('#name').attr('style', 'border: 2px solid red;');
		$('#name').focus();
	}
	else{
		$('#name').attr('style', 'border: none;');
	}

	if($('#email').val() == '' && !isValidEmailAddress($('#email'))){
		result = false;
		$('#email').attr('style', 'border: 2px solid red;');
		$('#email').focus();
	}
	else{
		$('#email').attr('style', 'border: none;');
	}

	if($('#language').val() == ''){
		result = false;
		$('#language').attr('style', 'border: 2px solid red;');
		$('#language').focus();
	}
	else{
		$('#language').attr('style', 'border: none;');
	}

	if($('#iso').val() == ''){
		result = false;
		$('#iso').attr('style', 'border: 2px solid red;');
		$('#iso').focus();
	}
	else{
		$('#iso').attr('style', 'border: none;');
	}

	return result;
};

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
    return pattern.test(emailAddress);
};

function enableEditForm(){

	$(".editTransInput").each(function( index ){
		$(this).removeAttr('disabled');
	});
	return false;
}